﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Linq;
using System.Concurrency;
using System.Collections.Generic;
using Caliburn.Micro;
using PicturePortfolio.ServiceReference;
using PicturePortfolio.Silverlight.Message;
namespace PicturePortfolio.Silverlight.ViewModels
{
    public class AddEditCategoryViewModel:PropertyChangedBase
    {
        PhotoServiceClient _service;
        public AddEditCategoryViewModel(PhotoServiceClient service)
        {
            _service = service;
            this.LoadCategories();
        }

        private List<Category> _existingCategories;
        public List<Category> ExistingCategories
        {
            get { return _existingCategories; }
            set
            {
                _existingCategories = value;
                NotifyOfPropertyChange(() => ExistingCategories);
            }
        }

        public string CategoryName { get; set; }
        public string Description { get; set; }
        public bool EnableDisableDeleteButton { get; private set; }
        private void LoadCategories()
        {
            var func = Observable.FromEvent<GetCategoriesCompletedEventArgs>(_service, "GetCategoriesCompleted")
                .ObserveOn(Scheduler.ThreadPool);
            _service.GetCategoriesAsync();

            func.ObserveOn(Scheduler.Dispatcher)
                .Select(r => r.EventArgs.Result)
                .Subscribe(s => this.ExistingCategories = s.ToList());
        }
        public void DeleteCategory()
        {
            _service.DeleteCategoryAsync(_selectedCategory);
            this.EnableDisableDeleteButton = false;
            this.ExistingCategories.Remove(_selectedCategory);
            _selectedCategory = null;
            NotifyOfPropertyChange(() => ExistingCategories);
            EventAggregationProvider.EventAggregator.Publish<CategoryAddedOrDeleted>(new CategoryAddedOrDeleted());
        }

        public void AddCategory()
        {
            Category category = new Category();
            category.CategoryName = this.CategoryName;
            
            //_service.s
            var func = Observable.FromEvent<System.ComponentModel.AsyncCompletedEventArgs>(_service, "SaveCategoryCompleted")
                .SubscribeOn(Scheduler.ThreadPool);
            _service.SaveCategoryAsync(category);
            func.SubscribeOn(Scheduler.Dispatcher)
                .Select(r => r.EventArgs)
                .Subscribe(s => this.SaveCategoryCompleted(s));
            this.ExistingCategories.Add(category);
            NotifyOfPropertyChange(() => ExistingCategories);
        }
        private void SaveCategoryCompleted(System.ComponentModel.AsyncCompletedEventArgs asyncCompletedEvent)
        {
            if (asyncCompletedEvent.Error == null)
            {
                EventAggregationProvider.EventAggregator.Publish<CategoryAddedOrDeleted>(new CategoryAddedOrDeleted());
                //MessageBox.Show("Category saved");
            }
            else
            {
                MessageBox.Show(asyncCompletedEvent.Error.Message);
            }
        }
        Category _selectedCategory;
        public void CategorySelectionChanged(object selectedItem)
        {
            Category category = selectedItem as Category;
            if (category != null)
            {
                this.EnableDisableDeleteButton = true;
                _selectedCategory = category;
            }
        }
    }
}
